/**  
 * @author renbangjie renbangjie@126.com  
 * @date 2016-4-15 上午10:39:10
 * @version V1.0  
 * <b>Copyright (c)</b> 2014医信金融信息服务（上海）有限公司-版权所有<br/>
 */
package com.yjd.bi.statistic.control;

import com.yjd.bi.comm.utils.BiDateUtil;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.user.service.*;
import com.yjd.comm.annotation.DateString;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户数相关统计
 * 
 * @author renbangjie renbangjie@126.com
 * @date 2016-4-15 上午10:39:10
 */
@Controller
@Scope("prototype")
@RequestMapping("user")
public class UserControl extends BaseControl {

	private final String rpmBiServiceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY;
    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IUserStatisticsService userStatisticsService;
    private IUserStatisticsRealTimeService userStatisticsRealTimeService;
    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private ILoginRetentionRateService loginRetentionRateService;
    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private ILoginRateDaysService loginRateDaysService;
    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private ILoginRateWeekService loginRateWeekService;
    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private ILoginRateMonthService loginRateMonthService;
    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IInvestRateDaysService investRateDaysService;
    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IInvestRateWeekService investRateWeekService;
    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IInvestRateMonthService investRateMonthService;

	/**
	 * 用户信息统计页面
     *
     * @return
     * @throws Exception
	 *             String
	 */
	@RequestMapping("index")
	public String index() throws Exception {
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("end", BiDateUtil.initBiDate());
        return "user.index";
	}

	/**
	 * 总览
     *
     * @throws Exception
     *             void
	 */
	@RequestMapping("summary")
	public void summary(@DateString String start, @DateString String end)
			throws Exception {
		Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
		paramMap.put("start", FrameUtil.getTime(DateUtil.parseDate(start,
				DateUtil.PATTERN_yyyyMMdd)));
		paramMap.put("end", FrameUtil.getTime(DateUtil.parseDate(end,
				DateUtil.PATTERN_yyyyMMdd))+23*60*60+59*61);

		Map<String, Integer> resultMap = new HashMap<String, Integer>();
		// 注册用户数
		paramMap.put("propertyName", "register_account");
		resultMap.put("register_account",
				userStatisticsService.getSummary(paramMap));
		// 提现用户数
		paramMap.put("propertyName", "first_cash");
		resultMap.put("first_cash",
				userStatisticsService.getSummary(paramMap));
		// 充值用户数
		paramMap.put("propertyName", "recharge_account");
		resultMap.put("recharge_account",
				userStatisticsService.getSummary(paramMap));
		// 投资用户数
		paramMap.put("propertyName", "invest_account");
		resultMap.put("invest_account",
				userStatisticsService.getSummary(paramMap));

		// 首次投资用户数
		paramMap.put("propertyName", "first_invest");
		resultMap.put("first_invest",
						userStatisticsService.getSummary(paramMap));

		// 首次充值用户数
		paramMap.put("propertyName", "first_recharge");
		resultMap.put("first_recharge",
						userStatisticsService.getSummary(paramMap));

		// 灵通卡激活人数
		paramMap.put("propertyName", "lck_activate");
		resultMap.put("lck_activate",
								userStatisticsService.getSummary(paramMap));


		//微信统计
		resultMap.put("wx_bind_count", userStatisticsService.wxBindCount(paramMap));
		super.successData(resultMap);
	}

	/**
	 * 统计
     *
     * @throws Exception
     *             void
	 */
	@RequestMapping("count")
	public void investCount(@RequestParam(required = false) Integer timeType,
			@DateString String timeFrom,
			@RequestParam(required = false) String field) throws Exception {
		EChartsModel echartsMoel = userStatisticsService.getDates(
				TimeType.getType(timeType), Long.valueOf(timeFrom), field);
		super.successData(FrameUtil.newHashMap("option", echartsMoel));
	}

	/**
	 * 实时数据
     *
     * @param timeFrom
     * @param field
	 * @throws Exception
	 *             void
	 */
	@RequestMapping("realTimeCount")
	public void getDailyDatas(String timeFrom, String field) throws Exception {
		timeFrom = DateUtil.timestampToDateString(DateUtil
				.dateStringToTimestamp(timeFrom, DateUtil.PATTERN_yyyy_MM_dd),
				"yyyyMMddHHmmss");
		EChartsModel ec = userStatisticsRealTimeService.getDates(
				Long.parseLong(timeFrom), field);
		this.successData(FrameUtil.newHashMap("option", ec));
	}

	/**
	 * 登录留存率
     *
     * @return
     * @throws Exception
	 *             String
	 */
	@RequestMapping("loginRate")
	public String loginRate(@RequestParam Integer skip) throws Exception {
		if (7 == skip) {
			request.setAttribute("resultMaps",
					loginRateDaysService.getLoginDaysRate(skip, 0L));
			return "userStatistic/login_rate_7days";
		}
		if (30 == skip) {
			request.setAttribute("resultMaps",
					loginRateDaysService.getLoginDaysRate(skip, 0L));
			return "userStatistic/login_rate_30days";
		}
		if (13 == skip) {
			request.setAttribute("resultMaps",
					loginRateWeekService.getLoginWeekRate(skip, 0L));
			return "userStatistic/login_rate_13week";
		}

		request.setAttribute("resultMaps",
				loginRateMonthService.getLoginMonthRate(skip, 0L));
		return "userStatistic/login_rate_12month";

	}

	/**
	 * 投资留存率
     *
     * @param skip
     * @return
	 * @throws Exception
	 *             String
	 */
	@RequestMapping("investRate")
	public String investRate(@RequestParam Integer skip) throws Exception {
		if (7 == skip) {
			request.setAttribute("resultMaps",
					investRateDaysService.getInvestDaysRate(skip, 0L));
			return "userStatistic/invest_rate_7days";
		}
		if (30 == skip) {
			request.setAttribute("resultMaps",
					investRateDaysService.getInvestDaysRate(skip, 0L));
			return "userStatistic/invest_rate_30days";
		}
		if (13 == skip) {
			request.setAttribute("resultMaps",
					investRateWeekService.getInvestWeekRate(skip, 0L));
			return "userStatistic/invest_rate_13week";
		}

		request.setAttribute("resultMaps",
				investRateMonthService.getInvestMonthRate(skip, 0L));
		return "userStatistic/invest_rate_12month";

	}

}
